/*
 * listt_client.h -- definition and declarations of client lst
 */
#include <stdlib.h>
#include <stdio.h>
#include "client.h"


/* queue_element_t */
typedef struct list_element{
    client_t *client;
    struct list_element *next;
} list_element_t;

/* queue_t */
typedef struct {
  list_element_t *first;
} list_client_t;

list_client_t* create_list_client(){
	list_client_t* lst=(list_client_t*)mallof(sizeof(list_client_t));
	lst->first=NULL;
	return lst;
}
void delete_list_client(list_client_t *lst){
	if( lst == NULL)return;
	list_element_t *px,*py;
	px = lst->first;
	while(px){
	py=px->next;
	client_free(px->client);
	free(px);
	px=py;	
	}
	free(lst);
}
int is_empty_list_client(list_client_t *lst){
	return (lst->first == NULL);
}
void insert_list_client(list_client_t *lst, client_t *client){
	if( (lst == NULL) || (client == NULL))return;
	list_element_t* node=(list_element_t*)mallof(sizeof(list_element_t));
	node->client=client;
	node->next=lst->first;
	lst->first=node;
}
void remove_list_client(list_client_t *lst, int client_id){
	if( lst == NULL)return;
	if( lst->first == NULL)return;
	list_element_t *px,*py;
	px=py=lst->first;
	if( px->client->client_id == client_id){
		lst->first=lst->first->next;
		client_free(px->client);
		free(px);
		return;
	}	
	for( py=px->next; py != NULL; px=py, py=py->next){
		if( py->client->client_id == client_id){
			px->next=py->next;
			client_free(py->client);
			free(py);
			return;
		}
	}
}

client_t* get_list_client(list_client_t *lst, int client_id){
	client_t* c=NULL;
	if( lst == NULL)return c;
	list_element_t *px,*py;
	px=lst->first;
	while(px){
		py=px->next;
		if( px->client->client_id == client_id){
			c=px->client;
			break;
		}
		px=py;
	}	
}


